<?php

class Silk_Zgb_Adminhtml_DealerController extends Mage_Adminhtml_Controller_Action
{
		protected function _initAction()
		{
				$this->loadLayout()->_setActiveMenu("zgb/dealer")->_addBreadcrumb(Mage::helper("adminhtml")->__("经销商管理"),Mage::helper("adminhtml")->__("经销商管理"));
				return $this;
		}
		public function indexAction() 
		{
			    $this->_title($this->__("轻松老板"));
			    $this->_title($this->__("经销商管理"));

				$this->_initAction();
				$this->renderLayout();
		}
		public function editAction()
		{			    
			    $this->_title($this->__("轻松老板"));
				$this->_title($this->__("经销商"));
			    $this->_title($this->__("修改"));
				
				$id = $this->getRequest()->getParam("id");
				$model = Mage::getModel("zgb/dealer")->load($id);
				if ($model->getId()) {
					Mage::register("dealer_data", $model);
					$this->loadLayout();
					$this->_setActiveMenu("zgb/dealer");
					$this->_addBreadcrumb(Mage::helper("adminhtml")->__("经销商管理"), Mage::helper("adminhtml")->__("经销商管理"));
					$this->_addBreadcrumb(Mage::helper("adminhtml")->__("经销商信息"), Mage::helper("adminhtml")->__("经销商信息"));
					$this->getLayout()->getBlock("head")->setCanLoadExtJs(true);
					$this->_addContent($this->getLayout()->createBlock("zgb/adminhtml_dealer_edit"))->_addLeft($this->getLayout()->createBlock("zgb/adminhtml_dealer_edit_tabs"));
					$this->renderLayout();
				} 
				else {
					Mage::getSingleton("adminhtml/session")->addError(Mage::helper("zgb")->__("经销商不存在."));
					$this->_redirect("*/*/");
				}
		}

		public function newAction()
		{

		$this->_title($this->__("轻松老板"));
		$this->_title($this->__("经销商"));
		$this->_title($this->__("新建"));

        $id   = $this->getRequest()->getParam("id");
		$model  = Mage::getModel("zgb/dealer")->load($id);

		$data = Mage::getSingleton("adminhtml/session")->getFormData(true);
		if (!empty($data)) {
			$model->setData($data);
		}

		Mage::register("dealer_data", $model);

		$this->loadLayout();
		$this->_setActiveMenu("zgb/dealer");

		$this->getLayout()->getBlock("head")->setCanLoadExtJs(true);

		$this->_addBreadcrumb(Mage::helper("adminhtml")->__("经销商管理"), Mage::helper("adminhtml")->__("经销商管理"));
		$this->_addBreadcrumb(Mage::helper("adminhtml")->__("经销商信息"), Mage::helper("adminhtml")->__("经销商信息"));


		$this->_addContent($this->getLayout()->createBlock("zgb/adminhtml_dealer_edit"))->_addLeft($this->getLayout()->createBlock("zgb/adminhtml_dealer_edit_tabs"));

		$this->renderLayout();

		}
		public function saveAction()
		{

			$post_data=$this->getRequest()->getPost();

			if ($post_data) {
				try {
					if(!$this->getRequest()->getParam("id"))
					{
						$admin = Mage::getModel("admin/user");
						$admin->setFirstname('经销商');
						$admin->setLastname('哈哈惠');
						$admin->setEmail(rand(0,999999).date('Ymd').'@hahahui.com');
						$admin->setUsername($this->getRequest()->getParam("login_name"));
						$admin->setPassword($this->getRequest()->getParam('password'));
						if($admin->userExists())
						{
							throw new Exception("登录名已存在");
						}

						$admin->save();

						$admin->setRoleIds(array(Mage::helper('zgb')->getRoleId('dealer')))
                        ->setRoleUserId($admin->getUserId())
                        ->saveRelations();
					}
					

					$model = Mage::getModel("zgb/dealer")
					->addData($post_data)
					->setId($this->getRequest()->getParam("id"));
					if(!$this->getRequest()->getParam("id"))
					{
						$model->setAdminId($admin->getId());
					}
					
					$model->save();

					$model->load($model->getId());
					if($model->getAdminId())
					{
						$admin = Mage::getModel('admin/user')->load($model->getAdminId());
						if($admin->getId() && (trim($post_data['login_name'])!=$admin->getUsername()))
						{
							$admin->setUsername($this->getRequest()->getParam("login_name"));
							$admin->save();
						}
					}

					Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("已保存！"));
					Mage::getSingleton("adminhtml/session")->setDealerData(false);

					if ($this->getRequest()->getParam("back")) {
						$this->_redirect("*/*/edit", array("id" => $model->getId()));
						return;
					}
					$this->_redirect("*/*/");
					return;
				} 
				catch (Exception $e) {
					Mage::getSingleton("adminhtml/session")->addError($e->getMessage());
					Mage::getSingleton("adminhtml/session")->setDealerData($this->getRequest()->getPost());
					$this->_redirect("*/*/edit", array("id" => $this->getRequest()->getParam("id")));
				return;
				}

			}
			$this->_redirect("*/*/");
		}



		public function deleteAction()
		{
				if( $this->getRequest()->getParam("id") > 0 ) {
					try {
						$model = Mage::getModel("zgb/dealer");
						$model->setId($this->getRequest()->getParam("id"))->delete();
						Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("已删除！"));
						$this->_redirect("*/*/");
					} 
					catch (Exception $e) {
						Mage::getSingleton("adminhtml/session")->addError($e->getMessage());
						$this->_redirect("*/*/edit", array("id" => $this->getRequest()->getParam("id")));
					}
				}
				$this->_redirect("*/*/");
		}

		
		public function massRemoveAction()
		{
			try {
				$ids = $this->getRequest()->getPost('ids', array());
				foreach ($ids as $id) {
                      $model = Mage::getModel("zgb/dealer");
					  $model->setId($id)->delete();
				}
				Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("已删除！"));
			}
			catch (Exception $e) {
				Mage::getSingleton("adminhtml/session")->addError($e->getMessage());
			}
			$this->_redirect('*/*/');
		}
			
		/**
		 * Export order grid to CSV format
		 */
		public function exportCsvAction()
		{
			$fileName   = 'dealer.csv';
			$grid       = $this->getLayout()->createBlock('zgb/adminhtml_dealer_grid');
			$this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
		} 
		/**
		 *  Export order grid to Excel XML format
		 */
		public function exportExcelAction()
		{
			$fileName   = 'dealer.xml';
			$grid       = $this->getLayout()->createBlock('zgb/adminhtml_dealer_grid');
			$this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
		}

		protected function _isAllowed()
	    {
	        return Mage::getSingleton('admin/session')->isAllowed('zgb');
	    }
}
